<?php

/* Portal Pracy - praca magisterska
 * autor: Tomek Ceszke
 * email: tomek.ceszke@gmail.com
 * Politechnika Koszalińska 2007
 */

class User extends AppModel
{
	var $name = 'User';

	var $belongsTo = 'Group';
	
    var $hasOne = array
    (
    	'Profile' => array
    	(
    		'className'  => 'Profile',
            'dependent'  =>  true,
            'foreignKey' => 'user_id'
         )
    );

    var $hasMany = 'Company';
		
	function loadValidation()
	{
		$this->validate = array
		(
			'username' => array
			(
				'required' => array
				(
					'method' => VALID_EMAIL,
          			'message' => 'Musisz podać prawidłowy e-mail.',
          		),
         		'length_within' => array
         		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać od 8 do 50 znaków.',
           			'parameters' => array('min' => 8, 'max' => 50)
           		)
           	) ,
           	'username_unique' => array
			(
				'required' => array
				(
					'method' => VALID_EMAIL,
          			'message' => 'Musisz podać prawidłowy e-mail.',
          		),
         		'length_within' => array
         		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać od 8 do 50 znaków.',
           			'parameters' => array('min' => 8, 'max' => 50)
           		),           		
           		'unique' => array
           		(
           			'method' => 'uniqueEmail',
           			'message' => 'Taki adres już istnieje. Spróbuj podać inny.',
           		)           		
           	) ,
           	'username_exist' => array
           	(
           		'required' => array
           		(
           			'method' => VALID_EMAIL,
           			'message' => 'Musisz podać prawidłowy e-mail.',
          		),
          		'length_within' => array
          		(
          			'method' => VALID_LENGTH_WITHIN,
          			'message' => 'To pole powinno zawierać od 8 do 50 znaków.',
          			'parameters' => array('min' => 8, 'max' => 50)
          		),
          		'exist' => array
          		(
          			'method' => 'existEmail',
          			'message' => 'Taki adres nie istnieje w bazie.',
          		)
           	) ,
           'group_id' => array
           (
           		'required' => array
           		(
           			'method' => VALID_NUMBER,
           		),
           	),
           'active' => array
           (
           		'required' =>            		
           		array
           		(
           			'method' => VALID_NUMBER,           	
           		),
           	),
           'passwd' => array
           (
           		'required' => array
           		(
					'method' => VALID_NOT_EMPTY, //TODO: zmienic na regexpa - paranoid
          			'message' => 'Musisz podać hasło.',
          		),
				'length_within' => array
				(
					'method' => VALID_LENGTH_WITHIN,
          			'message' => 'To pole powinno zawierać od 3 do 32 znaków.',
          			'parameters' => array('min' => 3, 'max' => 32)
          		),
          		'compare' => array
          		(
					'method' => 'comparePasswords',
          			'message' => 'Oba hasła muszą być takie same.',          			
          		)
          	),
          	'passwd_repeat' => array
           (
           		'required' => array
           		(
					'method' => VALID_NOT_EMPTY, //TODO: zmienic na regexpa - paranoid
          			'message' => 'Musisz podać hasło.',
          		),
				'length_within' => array
				(
					'method' => VALID_LENGTH_WITHIN,
          			'message' => 'To pole powinno zawierać od 3 do 32 znaków.',
          			'parameters' => array('min' => 3, 'max' => 32)
          		)
          	),
          	'captcha' => array
            ( 	
            	'required' => array
          		(
					'method' => '/^365$/',
          			'message' => 'Musisz podac dobry kod.'          			
          		)           
           	),                     			
		);
		
		
	}
	

		
	function uniqueEmail()
	{
		$email = $this->data['User']['username_unique'];
		$count = $this->findCount('username=\''.$email.'\'');

		if($count==0) 
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	function existEmail()
	{
		$email = $this->data['User']['username_exist'];
		$count = $this->findCount('username=\''.$email.'\'');

		if($count==0)
		{
			return false;
		}
		else
		{
			return true;
		}

	}

	function comparePasswords()
	{
		if(isset($this->data['User']['passwd_repeat']))
		{
			$passwdrepeat = $this->data['User']['passwd_repeat'];
			$passwd = $this->data['User']['passwd'];
			
			if($passwdrepeat == $passwd)
			{
				$this->data['User']['passwd'] = md5($this->data['User']['passwd']);
				return true;
			}
			else
			{
				return false;
			}
			
		}
		else
		{
			return true;
		}
	}
}

?>
